package com.youkang.youkangseleniumjava.controller;

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpClientExample {
    public static String post(String url1, Map<String, String> data,String type) {
        try {
            // 目标 URL
            URL url = new URL(url1);

            // 创建 HttpURLConnection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(type); // 或者使用 "GET"，"PUT"，"DELETE"，等
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            // 构建 JSON 数据
            // 使用 Jackson ObjectMapper 转换为 JSON 字符串
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonData = objectMapper.writeValueAsString(data);
            // 将 JSON 数据写入请求体
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应
            int responseCode = connection.getResponseCode();

            // 读取响应内容
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                return response.toString();
                // 输出响应内容
//                System.out.println("Response Content: " + response.toString());
            } else {
                // 处理错误流，如果需要
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                String line;
                StringBuilder errorResponse = new StringBuilder();

                while ((line = errorReader.readLine()) != null) {
                    errorResponse.append(line);
                }
                errorReader.close();
                connection.disconnect();
                return errorResponse.toString();
                // 输出错误内容
//                System.out.println("Error Response: " + errorResponse.toString());
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }





    public static String get(String url1, String type, String token, String cookie, JSONObject jsonData) {
        try {
            // 目标 URL
            // 创建 HttpURLConnection
            URL url = new URL(url1);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(type);
            // 设置多个头部信息
            connection.setRequestProperty("token", token);
            connection.setRequestProperty("Cookie","JSESSIONID="+cookie);
            // 将 JSON 数据写入请求体
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonData2 = objectMapper.writeValueAsString(jsonData);
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonData2.getBytes("utf-8");
                os.write(input, 0, input.length);
            }
            // 获取响应
            int responseCode = connection.getResponseCode();


            // 读取响应内容
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                reader.close();

                // 输出响应内容
                System.out.println("Response Content: " + response.toString());
            } else {
                // 处理错误流，如果需要
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                String line;
                StringBuilder errorResponse = new StringBuilder();

                while ((line = errorReader.readLine()) != null) {
                    errorResponse.append(line);
                }

                errorReader.close();

                // 输出错误内容
                System.out.println("Error Response: " + errorResponse.toString());
            }

            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

            return null;
        }

}
